


// datagrid行编辑模式
define("datagrid-lineEdit",
		function(require, exports, module) {

			// datagrid扩展工具栏及默认方法
			function getToolbarUI(uiData) {
				var mygrid = uiData.mygrid;
				var exToolBar = uiData.uiOpts.toolbarEx;
				// 扩展工具栏
				var toolbarNode = [ {
					eName : 'linkbutton',
					text : '新增',
					uId : 'add',
					plain : true,
					iconCls : 'icon-add'
				}, {
					eName : 'linkbutton',
					text : '编辑',
					uId : 'edit',
					plain : true,
					iconCls : 'icon-edit'
				}, {
					eName : 'linkbutton',
					text : '应用',
					uId : 'save',
					plain : true,
					iconCls : 'icon-save'
				}, {
					eName : 'linkbutton',
					text : '删除',
					uId : 'remove',
					plain : true,
					iconCls : 'icon-remove'
				} ];
				toolbarNode = $ui.extUIs(toolbarNode, exToolBar);
				$.each(toolbarNode, function(i, node) {// 遍历工具栏中按钮，加入默认方法
					var row = null;
					if (!node)
						return;// 空节点退出
					if (node.eName == 'linkbutton') {
						if (node.onClick == undefined)
							node.onClick = gettbclickfn(uiData, node.uId);
					} else if (node.eName == 'searchbox') {
						if (node.searcher == null) {
							node.searcher = function() {
								mygrid.datagrid('load');
							}
						}
					}
				});
				uiData.uiOpts.toolbarEx = toolbarNode;
				return toolbarNode;
			}

			// 工具栏默认事件
			function gettbclickfn(uiData, uId) {
				var mygrid = uiData.mygrid;
				var uiOpts = uiData.uiOpts;

				/*
				 * 1 判断是否为当前行 2 验证数据是否有效 3 取消本编辑行
				 */
				function cancelEdit(rowIndex) {
					if (uiOpts.editIndex < 0 || rowIndex == uiOpts.editIndex)
						return true;
					if (mygrid.datagrid('validateRow', uiOpts.editIndex)) {
						mygrid.datagrid('endEdit', uiOpts.editIndex);// 取消编辑
						uiOpts.editIndex = -1;// 重置
						return true;
					}
					return false;
				}

				var toolbarCick = {
					add : function() {// 新增
						if (false == cancelEdit())
							return;
						row = {};
						if (uiOpts.onBeforeAdd) {// 返回false，则终止继续
							if (false == uiOpts.onBeforeAdd.call(mygrid.get(0),
									row)) {
								return;
							}
						}
						mygrid.datagrid('appendRow', row);
						var lastIndex = mygrid.datagrid('getRows').length - 1;
						mygrid.datagrid('selectRow', lastIndex);
						mygrid.datagrid('beginEdit', lastIndex);
						uiOpts.editIndex = lastIndex;
					},
					edit : function() {// 修改
						if (false == cancelEdit())
							return;
						row = mygrid.datagrid('getSelected');
						if (row == null) {
							$ui.alert('请选择一条要修改的记录 !');
							return;
						}
						if (uiOpts.onBeforeModify) {// 返回false，则终止继续
							if (false == uiOpts.onBeforeModify.call(mygrid
									.get(0), row)) {
								return;
							}
						}
						uiOpts.editIndex = mygrid.datagrid('getRowIndex', row);
						mygrid.datagrid('beginEdit', uiOpts.editIndex);
					},
					save : function() {
						// 关闭编辑模式
						if (uiOpts.editIndex > -1
								&& mygrid.datagrid('validateRow', uiOpts.editIndex)) {
							mygrid.datagrid('endEdit', uiOpts.editIndex);// 取消编辑
							mygrid.datagrid('acceptChanges');
							uiOpts.editIndex = -1;// 重置
							return true;
						}
					},
					remove : function() {// 默认删除方法
						row = mygrid.datagrid('getSelected');
						if (row == null) {
							$ui.alert('请选择一条要删除的记录 !');
							return;
						}
						if (uiOpts.onBeforeDel) {// 返回false，则终止继续
							if (false == uiOpts.onBeforeDel
									.call(mygrid[0], row))
								return;
						}
						var index = -1;
						if (uiOpts.editIndex == -1) {
							index = mygrid.datagrid('getRowIndex', row);
						} else
							index = uiOpts.editIndex;
						mygrid.datagrid('deleteRow', index);
					}
				};
				return toolbarCick[uId];
			}

			// 返回工厂函数
			return function(mygrid, uiOpts) {
				return getToolbarUI({
					mygrid : mygrid,
					uiOpts : uiOpts
				});
			}
});

